package net.mikoo.seals.api.model.verify;

import java.text.SimpleDateFormat;
import java.util.stream.Collectors;

import com.sun.mail.imap.protocol.Item;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.api.utility.ApiConstants;
import net.mikoo.seals.common.enums.ApplyTypeEnum;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.ApplyVerify;

public class VerifyDetailResponse extends BaseResponse {

	private static final long serialVersionUID = 2156606084098098263L;
	
	private VerifyDetail verifyInfo;

	public VerifyDetail getVerifyInfo() {
		return verifyInfo;
	}

	public void setVerifyInfo(VerifyDetail verifyInfo) {
		this.verifyInfo = verifyInfo;
	}
	
	public static VerifyDetailResponse valueOf(ApplyVerify verify) {
		
		if(verify == null) {
			throw new IllegalArgumentException("verifyDetail");
		}
		VerifyDetailResponse result = new VerifyDetailResponse();
		VerifyDetail verifyInfo = new VerifyDetail();
		
		if(verify.getApply() != null) {
			Apply apply = verify.getApply();
			verifyInfo.setApplyId(apply.getApplyId());
			verifyInfo.setApplyPartyName(apply.getPerson().getName());
			verifyInfo.setPortrait(apply.getPerson().getPortrait());
			ApplyTypeEnum applyType = ApplyTypeEnum.getByCode(apply.getApplyType());
			if(applyType != null) {
				verifyInfo.setApplyTypeName(applyType.getMessage());
				verifyInfo.setApplyType(applyType.getCode());
			}
			verifyInfo.setCause(apply.getCause());
			
			verifyInfo.setSealName(apply.getSeal().getSealName());
			verifyInfo.setStatusName("待审核");
			verifyInfo.setTimes(apply.getApplyTimes());
			
			verifyInfo.setApplyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(apply.getApplyTime()));
			
			if(applyType == ApplyTypeEnum.TAKE_APPLY) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				verifyInfo.setUseStartTime(sdf.format(apply.getTakeStartTime()));
				verifyInfo.setUseEndTime(sdf.format(apply.getTakeEndTime()));
			}
			verifyInfo.setVerifyId(verify.getApplyVerifyId());
			if(apply.getUpfiles() != null) {
				verifyInfo.setVouchers(apply.getUpfiles().stream().map(item -> item.getFileUrl()).collect(Collectors.toList()));
			}
			
			verifyInfo.setAddress(apply.getTakeDestination());
		}
		
		result.setVerifyInfo(verifyInfo);
		return result;
	}
	
}
